/**
 * Copyright &copy; 2012-2014 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.employee.web;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.util.List;
import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.utils.CalendarUtils;
import com.thinkgem.jeesite.common.utils.Constons.HolidayType;
import com.thinkgem.jeesite.common.utils.DateUtils;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.common.utils.excel.ExportExcel;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.employee.dao.EmployeeLeaveDao;
import com.thinkgem.jeesite.modules.employee.dao.StatutoryHolidaysDao;
import com.thinkgem.jeesite.modules.employee.entity.CountOutsourceCost;
import com.thinkgem.jeesite.modules.employee.entity.EmployeeLeave;
import com.thinkgem.jeesite.modules.employee.entity.LogisticsCost;
import com.thinkgem.jeesite.modules.employee.entity.OutsourceCostDetail;
import com.thinkgem.jeesite.modules.employee.entity.SummaryAttendance;
import com.thinkgem.jeesite.modules.employee.service.OutsourceCostDetailService;

/**
 * 外包工考勤成本明细表Controller
 * @author pan.zhou
 * @version 2017-07-26
 */
@Controller
@RequestMapping(value = "${adminPath}/employee/outsourceCostDetail")
public class OutsourceCostDetailController extends BaseController {

	@Autowired
	private OutsourceCostDetailService outsourceCostDetailService;
	@Autowired
    protected EmployeeLeaveDao employeeLeaveDao;

	@Autowired
    protected StatutoryHolidaysDao statutoryHolidaysDao;

	@ModelAttribute
	public OutsourceCostDetail get(@RequestParam(required=false) String id) {
		OutsourceCostDetail entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = outsourceCostDetailService.get(id);
		}
		if (entity == null){
			entity = new OutsourceCostDetail();
		}
		return entity;
	}
	
	@RequiresPermissions("employee:outsourceCostDetail:view")
	@RequestMapping(value = {"list", ""})
	public String list(OutsourceCostDetail outsourceCostDetail, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<OutsourceCostDetail> page = outsourceCostDetailService.findPage(new Page<OutsourceCostDetail>(request, response), outsourceCostDetail); 
		model.addAttribute("page", page);
		return "modules/employee/outsourceCostDetailList";
	}

	@RequiresPermissions("employee:outsourceCostDetail:view")
	@RequestMapping(value = "form")
	public String form(OutsourceCostDetail outsourceCostDetail, Model model) {
		model.addAttribute("outsourceCostDetail", outsourceCostDetail);
		return "modules/employee/outsourceCostDetailForm";
	}

	@RequiresPermissions("employee:outsourceCostDetail:edit")
	@RequestMapping(value = "save")
	public String save(OutsourceCostDetail outsourceCostDetail, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, outsourceCostDetail)){
			return form(outsourceCostDetail, model);
		}
		outsourceCostDetailService.save(outsourceCostDetail);
		addMessage(redirectAttributes, "保存外包工考勤成本明细表成功");
		return "redirect:"+Global.getAdminPath()+"/employee/outsourceCostDetail/?repage";
	}
	
	@RequiresPermissions("employee:outsourceCostDetail:edit")
	@RequestMapping(value = "delete")
	public String delete(OutsourceCostDetail outsourceCostDetail, RedirectAttributes redirectAttributes) {
		outsourceCostDetailService.delete(outsourceCostDetail);		addMessage(redirectAttributes, "删除外包工考勤成本明细表成功");
		return "redirect:"+Global.getAdminPath()+"/employee/outsourceCostDetail/?repage";
	}
	
	@RequiresPermissions("employee:outsourceCostDetail:view")
    @RequestMapping(value = {"countOutsourceCost", ""})
    public String countOutsourceCost(CountOutsourceCost outsourceCostDetail, HttpServletRequest request, HttpServletResponse response, Model model) {
	    if(outsourceCostDetail.getStartDate()==null||outsourceCostDetail.getEndDate()==null){
	        outsourceCostDetail.setStartDate(new Date());
	        outsourceCostDetail.setEndDate(new Date());
	    }
        Page<CountOutsourceCost> page = outsourceCostDetailService.queryCountOutsourcePage(new Page<CountOutsourceCost>(request, response), outsourceCostDetail); 
        model.addAttribute("page", page);
        model.addAttribute("startDate",outsourceCostDetail.getStartDate());
        model.addAttribute("endDate",outsourceCostDetail.getEndDate());
        return "modules/employee/countOutsourceCostList";
    }
	
	
	@RequiresPermissions("employee:outsourceCostDetail:view")
    @RequestMapping(value = {"queryLogisticsCost", ""})
    public String queryLogisticsCost(LogisticsCost logisticsCost, HttpServletRequest request, HttpServletResponse response, Model model) {
        Page<LogisticsCost> page = outsourceCostDetailService.queryLogisticsCostPage(new Page<LogisticsCost>(request, response), logisticsCost); 
        model.addAttribute("page", page);        
        return "modules/employee/logisticsCostList";
    }
    
    
    @RequiresPermissions("employee:outsourceCostDetail:view")
    @RequestMapping(value = {"summaryAttendance", ""})
    public String summaryAttendance(SummaryAttendance summaryAttendance, HttpServletRequest request, HttpServletResponse response, Model model) {
        if(summaryAttendance.getStartDate()==null||summaryAttendance.getEndDate()==null){
            summaryAttendance.setStartDate(new Date());
            summaryAttendance.setEndDate(new Date());
        }
        Page<SummaryAttendance> page = outsourceCostDetailService.querySummaryAttendancePage(new Page<SummaryAttendance>(request, response), summaryAttendance);

        model.addAttribute("startDate",summaryAttendance.getStartDate());
        model.addAttribute("endDate",summaryAttendance.getEndDate());
        model.addAttribute("page", page);        
        return "modules/employee/summaryAttendanceList";
    }










	/**
	 *
	 * @param EmployeeLeave
	 * @param request
	 * @param response
	 * @param redirectAttributes
	 * @return
	 */
	@RequiresPermissions("employee:outsourceCostDetail:view")
	@RequestMapping(value = "export", method= RequestMethod.POST)
	public String exportFile(OutsourceCostDetail attendanceRecords, HttpServletRequest request, HttpServletResponse response, RedirectAttributes redirectAttributes) {
		try {
			String fileName = "外包工考勤成本统计信息数据"+ DateUtils.getDate("yyyyMMddHHmmss")+".xlsx";
			Page<OutsourceCostDetail> page = outsourceCostDetailService.findPage(new Page<OutsourceCostDetail>(request, response, -1), attendanceRecords);
			new ExportExcel("外包工考勤成本统计信息数据", CountOutsourceCost.class).setDataList(page.getList()).write(response, fileName).dispose();
			return null;
		} catch (Exception e) {
			addMessage(redirectAttributes, "导出外包工考勤成本统计信息失败！失败信息："+e.getMessage());
		}
		return "redirect:" + adminPath + "/employee/outsourceCostDetail/list?repage";
	}

}